#pragma once
#include "Entity.h"
#include "Engine/Engine.h"
#include "Item/ItemFactory.h"

/*
Mania: Desire to wander.
Aggression: Desire to attack.
Discipline: Desire to flock

*/

void Entity::NPC_HandleAI(Vector<Entity*> * a_EntityList)
{
	if(alive == false)
			Momentums[0].x = 0;
	else
	{
		//Every two seconds in game, find a new target and recalculate the boundries of MAD
		if(twoSecondTimer.update())		
		{
			NPC_RecalcAI();
			NPC_RefreshTarget(a_EntityList);
		}

		/*Calculate the dominant mental trait*/
		//else if(unsigned(Mania) >= unsigned(Aggression) && unsigned(Mania) >=  unsigned(Discipline))
			//NPC_HandleManic(a_EntityList);
		//else if(unsigned(Aggression) >= unsigned(Mania) && unsigned(Aggression) >= unsigned(Discipline))
		NPC_HandleAngry(a_EntityList);
		//else    NPC_HandleStoic(a_EntityList);
	}
}

void Entity::NPC_RecalcAI()
{
	wanderRange	= Mania * 2;					wanderRange *=	wanderRange;
	agroSphere = Aggression * 5;				agroSphere *=	agroSphere;
	flockRange	= Discipline;					flockRange	*=	flockRange;
}

//See if there is a closer target, if there is switch the current target to the new one
void Entity::NPC_RefreshTarget(Vector<Entity*> * a_EntityList)
{
	indexofTarget = -1;
	for(int i = 0; i < a_EntityList->size(); i++)
		if(a_EntityList->getByIndex(i)->getEntType() == P_TYPE_PLAYER)
			if(NPC_PollDistance(a_EntityList->getByIndex(i)) < agroSphere)
 				indexofTarget = i;
}
//This function is used to return the distance between the AI and it's target
//Used the Pyth Therom
int Entity::NPC_PollDistance(Entity * Target)
{
	Vector3D targetPos	=	Target->getPos();

	int a = (int)(targetPos.x - m_v3Pos.x);
	int b = (int)(targetPos.y - m_v3Pos.y);
	int csquared = (a*a) + (b*b);
	return csquared;
}
//~~~~~~~~ End the NPC Functions

//~~~~~~~~ Begin the AI patterns
//Find way to the target, if you are hitting a wall, jump over it and continue to move towards target
void Entity::AI_TrackTarget(Vector3D a_target)
{
	int a = (int)(a_target.x - m_v3Pos.x);
	int b = (int)(a_target.y - m_v3Pos.y);


	if(CollidedDirections[DIR_LEFT] || CollidedDirections[DIR_RIGHT] && humanReactionTimer.update())
		MovementQueue.push(UP);
	else
	{
		if(a > 0)
			MovementQueue.push(RIGHT);
		else
			MovementQueue.push(LEFT);	
	}
}

void Entity::NPC_HandleManic(Vector<Entity*> * a_EntityList)
{


};
void Entity::NPC_HandleAngry(Vector<Entity*> * a_EntityList)
{
	//Every frame, poll the distance between this npc and it's target
	distancetoTarget = NPC_PollDistance(a_EntityList->getByIndex(indexofTarget));
	//Determine if the target is within this npc's 'anger circle'

	//If the player is within the agro range and is quite outside of the attack range
	if(distancetoTarget <= agroSphere && distancetoTarget > (wornEquipment[Weaponry]->getRange() * wornEquipment[Weaponry]->getRange()) *.25) 
	{
		AI_TrackTarget(a_EntityList->getByIndex(indexofTarget)->getPos()); 	//call the track function
	}
	//Once the player is within attacking range begin to swing
	if(distancetoTarget < (wornEquipment[Weaponry]->getRange() * wornEquipment[Weaponry]->getRange())*.25)
	{
		AI_TargetPos = a_EntityList->getByIndex(indexofTarget)->getPos();
		if(humanReactionTimer.update())
			ActivityQueue.push(SWING);
		Momentums[0].x *= 0.1f;
	}

}
void Entity::NPC_HandleStoic(Vector<Entity*> * a_EntityList)
{

}
/*
Mania 
	10: +5  Block Wander														-10: -5 
	20:	+10 Block Wander														-20: -10
	30:	+20 Block Wander														-30: -20 (No Wander)
							+-40: Self preservation begins to fail 
							May prevent them losing aggro when near death.								
	50:	Random Digging and Climbing												-50:Takes random pierce and impact damage (1 or 2) from self immolation

		At this point, they stop equipping items.

	60: No Self Preservation													-60:Sluggish, never runs anywhere
	70:																			-70:Attack players unarmed
	80:																			-80:Take 4-5 random pierce damage sporadically from starvation
	90:																			-90:Reacts to nothing
	100:Runs around randomly, pushing random behaviors back. May randomly die.	-100: Stops breathing.

Aggression:

	10-30 increases or decreases of 5,10 and 20 to aggro radius.
	40: Lessened Care for self.							-40: Always seeks far range
	50: Always seeks close range						-50: Flees conflict		
	60: No self preservation							-60: 

Determine which M.A.D is highest.
*/